﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace RFW.Silverlight.Weather
{
	public partial class DigitalDisplay : UserControl
	{
		public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(double), typeof(DigitalDisplay), new PropertyMetadata(new PropertyChangedCallback(onValueChanged)));
		public static readonly DependencyProperty NumberOfDigitsProperty = DependencyProperty.Register("NumberOfDigits", typeof(Size), typeof(DigitalDisplay), new PropertyMetadata(new PropertyChangedCallback(onNumberOfDigitsChanged)));

		List<DigitalNumber> _numbers = new List<DigitalNumber>();
		Grid _grdDecimal = new Grid();

		private int _decimalColumn = -1;
		private int _decimalPlaces = 0;

		public DigitalDisplay()
		{
			InitializeComponent();
		}

		public double Value
		{
			get { return (double)this.GetValue(ValueProperty); }
			set
			{
				this.SetValue(ValueProperty, value);
				DisplayValue(this.Value);
			}
		}

		public Size NumberOfDigits
		{
			get { return (Size)this.GetValue(NumberOfDigitsProperty); }
			set
			{
				this.SetValue(NumberOfDigitsProperty, value);
				UpdateDisplay(this.NumberOfDigits);
				DisplayValue(this.Value);
			}
		}

		private void UpdateDisplay(Size size)
		{
			grdNumbers.Children.Clear();
			grdNumbers.ColumnDefinitions.Clear();
			grdNumbers.RowDefinitions.Clear();
			_numbers.Clear();

			grdNumbers.RowDefinitions.Add(new RowDefinition());
			grdNumbers.RowDefinitions.Add(new RowDefinition());

			grdNumbers.RowDefinitions[0].Height = new GridLength(5, GridUnitType.Star);
			grdNumbers.RowDefinitions[1].Height = new GridLength(9, GridUnitType.Pixel);

			grdNumbers.RowDefinitions.Add(new RowDefinition());
			
			//	Setup Ints
			int digit = 0;
			int column = 0;
			for (int col = 0; col < size.Width; col++)
			{
				grdNumbers.ColumnDefinitions.Add(new ColumnDefinition());
				_numbers.Add(new DigitalNumber());

				DigitalNumber number = _numbers[digit++];
				number.Margin = new Thickness(2);
				number.SetValue(Grid.ColumnProperty, column++);
				number.SetValue(Grid.RowProperty, 0);
				number.SetValue(Grid.RowSpanProperty, 2);

				grdNumbers.Children.Add(number);
			}

			//	Setup Decimal
			_decimalPlaces = (int)size.Height;
			if (_decimalPlaces > 0)
			{
				grdNumbers.ColumnDefinitions.Add(new ColumnDefinition());
				grdNumbers.ColumnDefinitions[column].Width = new GridLength(9, GridUnitType.Pixel);
				_decimalColumn = column;
				
				_grdDecimal.Margin = new Thickness(2);
				_grdDecimal.SetValue(Grid.ColumnProperty, column++);
				_grdDecimal.SetValue(Grid.RowProperty, 1);
				_grdDecimal.Background = new SolidColorBrush(Colors.Black);

				grdNumbers.Children.Add(_grdDecimal);

				for (int col = 0; col < _decimalPlaces; col++)
				{
					grdNumbers.ColumnDefinitions.Add(new ColumnDefinition());
					_numbers.Add(new DigitalNumber());

					DigitalNumber number = _numbers[digit++];
					number.Margin = new Thickness(2);
					number.SetValue(Grid.ColumnProperty, column++);
					number.SetValue(Grid.RowProperty, 0);
					number.SetValue(Grid.RowSpanProperty, 2);

					grdNumbers.Children.Add(number);
				}
			}
		}

		private void DisplayValue(double value)
		{
			value = Math.Round(value, _decimalPlaces);
			string digits = value.ToString();
			int decimalPlace = digits.IndexOf('.') - 1;

			if (decimalPlace < 0)
				decimalPlace = digits.Length - 1;

			if (_decimalColumn < 0)
				_decimalColumn = _numbers.Count;

			//	Process Int
			int number = _decimalColumn - 1;
			for (int pos = decimalPlace; pos >= 0 && number >= 0; pos--)
				_numbers[number--].Value = Convert.ToInt32(digits.Substring(pos, 1));

			while (number >= 0)
				_numbers[number--].Value = -1;

			//	Process Decimal
			if (_decimalPlaces > 0)
			{
				number = _decimalColumn;
				for (int pos = decimalPlace + 2; pos < digits.Length && number < _numbers.Count; pos++)
					_numbers[number++].Value = Convert.ToInt32(digits.Substring(pos, 1));

				while (number < _numbers.Count)
					_numbers[number++].Value = 0;
			}
		}

		private static void onValueChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
		{
		}

		private static void onNumberOfDigitsChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
		{
		}
	}
}
